{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f9344b4",
   "metadata": {},
   "source": [
    "## Anchor Tabular Explainer for SKLearn Income Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bed5b232",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: income\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/examples/mlserver_1.3.5/income/classifier\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/income.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e65edf6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/income.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b1a65e4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model status income -w ModelAvailable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1729d991",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"income_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"c65b8302-85af-4bac-aac5-91e3bedebee8\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t0\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer income \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 12], \"datatype\": \"FP32\", \"data\": [[47,4,1,1,1,3,4,1,0,0,40,9]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "843a5a70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: income-explainer\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/examples/mlserver_1.3.5/income/explainer\"\r\n",
      "  explainer:\r\n",
      "    type: anchor_tabular\r\n",
      "    modelRef: income\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/income-explainer.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "73de58f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/income-explainer.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "10e50de8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model status income-explainer -w ModelAvailable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6191c534",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"income-explainer_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"a22c3785-ff3b-4504-9b3c-199aa48a62d6\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"explanation\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"BYTES\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"str\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t\"{\\\"meta\\\": {\\\"name\\\": \\\"AnchorTabular\\\", \\\"type\\\": [\\\"blackbox\\\"], \\\"explanations\\\": [\\\"local\\\"], \\\"params\\\": {\\\"seed\\\": 1, \\\"disc_perc\\\": [25, 50, 75], \\\"threshold\\\": 0.95, \\\"delta\\\": 0.1, \\\"tau\\\": 0.15, \\\"batch_size\\\": 100, \\\"coverage_samples\\\": 10000, \\\"beam_size\\\": 1, \\\"stop_on_first\\\": false, \\\"max_anchor_size\\\": null, \\\"min_samples_start\\\": 100, \\\"n_covered_ex\\\": 10, \\\"binary_cache_size\\\": 10000, \\\"cache_margin\\\": 1000, \\\"verbose\\\": false, \\\"verbose_every\\\": 1, \\\"kwargs\\\": {}}, \\\"version\\\": \\\"0.9.0\\\"}, \\\"data\\\": {\\\"anchor\\\": [\\\"Marital Status = Never-Married\\\", \\\"Relationship = Own-child\\\"], \\\"precision\\\": 0.9518716577540107, \\\"coverage\\\": 0.07165109034267912, \\\"raw\\\": {\\\"feature\\\": [3, 5], \\\"mean\\\": [0.7959381044487428, 0.9518716577540107], \\\"precision\\\": [0.7959381044487428, 0.9518716577540107], \\\"coverage\\\": [0.3037383177570093, 0.07165109034267912], \\\"examples\\\": [{\\\"covered_true\\\": [[52, 5, 5, 1, 8, 1, 2, 0, 0, 0, 50, 9], [49, 4, 1, 1, 4, 4, 1, 0, 0, 0, 40, 1], [23, 4, 1, 1, 6, 1, 4, 1, 0, 0, 40, 9], [55, 2, 1, 1, 5, 1, 4, 0, 0, 0, 48, 9], [22, 4, 1, 1, 2, 3, 4, 0, 0, 0, 15, 9], [51, 4, 2, 1, 5, 0, 1, 1, 0, 0, 99, 4], [40, 4, 1, 1, 5, 1, 4, 0, 0, 0, 40, 9], [40, 6, 1, 1, 2, 0, 4, 1, 0, 0, 50, 9], [50, 5, 5, 1, 6, 0, 4, 1, 0, 0, 55, 9], [41, 4, 1, 1, 6, 0, 4, 1, 0, 0, 40, 9]], \\\"covered_false\\\": [[42, 4, 1, 1, 8, 0, 4, 1, 0, 2415, 60, 9], [48, 6, 2, 1, 5, 4, 4, 0, 0, 0, 60, 9], [37, 4, 1, 1, 5, 0, 4, 1, 0, 0, 45, 9], [57, 4, 5, 1, 8, 0, 4, 1, 0, 0, 50, 9], [63, 7, 2, 1, 8, 0, 4, 1, 0, 1902, 50, 9], [51, 4, 5, 1, 8, 0, 4, 1, 0, 1887, 47, 9], [51, 2, 2, 1, 8, 1, 4, 0, 0, 0, 45, 9], [68, 7, 5, 1, 5, 0, 4, 1, 0, 2377, 42, 0], [45, 4, 1, 1, 8, 0, 4, 1, 15024, 0, 40, 9], [45, 4, 1, 1, 8, 0, 4, 1, 0, 1977, 60, 9]], \\\"uncovered_true\\\": [], \\\"uncovered_false\\\": []}, {\\\"covered_true\\\": [[44, 6, 5, 1, 8, 3, 4, 0, 0, 1902, 60, 9], [58, 7, 2, 1, 5, 3, 1, 1, 4064, 0, 40, 1], [50, 7, 1, 1, 1, 3, 2, 0, 0, 0, 37, 9], [34, 4, 2, 1, 5, 3, 4, 1, 0, 0, 45, 9], [45, 4, 1, 1, 5, 3, 4, 1, 0, 0, 40, 9], [33, 7, 5, 1, 5, 3, 1, 1, 0, 0, 30, 6], [61, 7, 2, 1, 5, 3, 4, 1, 0, 0, 40, 0], [35, 4, 5, 1, 1, 3, 4, 1, 0, 0, 40, 9], [71, 2, 1, 1, 5, 3, 4, 0, 0, 0, 6, 9], [44, 4, 1, 1, 8, 3, 2, 1, 0, 0, 35, 9]], \\\"covered_false\\\": [[30, 4, 5, 1, 5, 3, 4, 1, 10520, 0, 40, 9], [54, 7, 2, 1, 8, 3, 4, 1, 0, 1902, 50, 9], [66, 6, 2, 1, 6, 3, 4, 1, 0, 2377, 25, 9], [35, 4, 2, 1, 5, 3, 4, 1, 7298, 0, 40, 9], [44, 4, 1, 1, 8, 3, 4, 1, 7298, 0, 48, 9], [31, 4, 1, 1, 8, 3, 4, 0, 13550, 0, 50, 9], [35, 4, 1, 1, 8, 3, 4, 1, 8614, 0, 45, 9]], \\\"uncovered_true\\\": [], \\\"uncovered_false\\\": []}], \\\"all_precision\\\": 0, \\\"num_preds\\\": 1000000, \\\"success\\\": true, \\\"names\\\": [\\\"Marital Status = Never-Married\\\", \\\"Relationship = Own-child\\\"], \\\"prediction\\\": [0], \\\"instance\\\": [47.0, 4.0, 1.0, 1.0, 1.0, 3.0, 4.0, 1.0, 0.0, 0.0, 40.0, 9.0], \\\"instances\\\": [[47.0, 4.0, 1.0, 1.0, 1.0, 3.0, 4.0, 1.0, 0.0, 0.0, 40.0, 9.0]]}}}\"\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer income-explainer \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 12], \"datatype\": \"FP32\", \"data\": [[47,4,1,1,1,3,4,1,0,0,40,9]]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a8d8b077",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload income-explainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bce80764",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload income"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "167c25aa",
   "metadata": {},
   "source": [
    "## Anchor Text Explainer for SKLearn Movies Sentiment Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc92f3fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: sentiment\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/mlserver_1.3.5/moviesentiment-sklearn\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/moviesentiment.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "179d9d68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/moviesentiment.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1dce2d5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model status sentiment -w ModelAvailable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9e82d700",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"sentiment_2\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"f5c07363-7e9d-4f09-aa30-228c81fdf4a4\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t0\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer sentiment \\\n",
    "  '{\"parameters\": {\"content_type\": \"str\"}, \"inputs\": [{\"name\": \"foo\", \"data\": [\"I am good\"], \"datatype\": \"BYTES\",\"shape\": [1]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "120af99f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: sentiment-explainer\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/mlserver_1.3.5/moviesentiment-sklearn-explainer\"\r\n",
      "  explainer:\r\n",
      "    type: anchor_text\r\n",
      "    modelRef: sentiment\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/moviesentiment-explainer.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d8fbec66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/moviesentiment-explainer.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c21e49f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model status sentiment-explainer -w ModelAvailable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ed7d4892",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: V2 server error: 500 Traceback (most recent call last):\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/middleware/errors.py\", line 162, in __call__\r\n",
      "    await self.app(scope, receive, _send)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette_exporter/middleware.py\", line 307, in __call__\r\n",
      "    await self.app(scope, receive, wrapped_send)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/middleware/gzip.py\", line 24, in __call__\r\n",
      "    await responder(scope, receive, send)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/middleware/gzip.py\", line 44, in __call__\r\n",
      "    await self.app(scope, receive, self.send_with_gzip)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/middleware/exceptions.py\", line 79, in __call__\r\n",
      "    raise exc\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/middleware/exceptions.py\", line 68, in __call__\r\n",
      "    await self.app(scope, receive, sender)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py\", line 21, in __call__\r\n",
      "    raise e\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py\", line 18, in __call__\r\n",
      "    await self.app(scope, receive, send)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/routing.py\", line 706, in __call__\r\n",
      "    await route.handle(scope, receive, send)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/routing.py\", line 276, in handle\r\n",
      "    await self.app(scope, receive, send)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/starlette/routing.py\", line 66, in app\r\n",
      "    response = await func(request)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/mlserver/rest/app.py\", line 42, in custom_route_handler\r\n",
      "    return await original_route_handler(request)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/fastapi/routing.py\", line 237, in app\r\n",
      "    raw_response = await run_endpoint_function(\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/fastapi/routing.py\", line 163, in run_endpoint_function\r\n",
      "    return await dependant.call(**values)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/mlserver/rest/endpoints.py\", line 99, in infer\r\n",
      "    inference_response = await self._data_plane.infer(\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/mlserver/handlers/dataplane.py\", line 103, in infer\r\n",
      "    prediction = await model.predict(payload)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/mlserver_alibi_explain/runtime.py\", line 86, in predict\r\n",
      "    output_data = await self._async_explain_impl(input_data, payload.parameters)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/mlserver_alibi_explain/runtime.py\", line 119, in _async_explain_impl\r\n",
      "    explanation = await loop.run_in_executor(self._executor, explain_call)\r\n",
      "  File \"/opt/conda/lib/python3.8/concurrent/futures/thread.py\", line 57, in run\r\n",
      "    result = self.fn(*self.args, **self.kwargs)\r\n",
      "  File \"/opt/conda/lib/python3.8/site-packages/mlserver_alibi_explain/explainers/black_box_runtime.py\", line 62, in _explain_impl\r\n",
      "    input_data = input_data[0]\r\n",
      "KeyError: 0\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer sentiment-explainer \\\n",
    "  '{\"parameters\": {\"content_type\": \"str\"}, \"inputs\": [{\"name\": \"foo\", \"data\": [\"I am good\"], \"datatype\": \"BYTES\",\"shape\": [1]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "55e7a86b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload sentiment-explainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4ce494ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload sentiment"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "vscode": {
   "interpreter": {
    "hash": "e08638e9ca1552ac93b876f0aeccba35125afc40c35f738854ef3f566228f7ad"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
